<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>环保社区 - 垃圾分类系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, #4ECDC4 0%, #44A08D 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
        }

        .content {
            padding: 30px;
        }

        .back-btn {
            display: inline-block;
            background: #6c757d;
            color: white;
            text-decoration: none;
            padding: 10px 20px;
            border-radius: 5px;
            margin-bottom: 30px;
            transition: all 0.3s;
        }

        .back-btn:hover {
            background: #5a6268;
            text-decoration: none;
            color: white;
        }

        .forum-actions {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            flex-wrap: wrap;
            gap: 15px;
        }

        .new-post-btn {
            background: linear-gradient(135deg, #4ECDC4 0%, #44A08D 100%);
            color: white;
            text-decoration: none;
            padding: 12px 25px;
            border-radius: 25px;
            font-size: 16px;
            transition: all 0.3s;
        }

        .new-post-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(78, 205, 196, 0.3);
            text-decoration: none;
            color: white;
        }

        .search-box {
            display: flex;
            align-items: center;
            background: #f8f9fa;
            border-radius: 25px;
            padding: 5px 20px;
            border: 2px solid #e9ecef;
            transition: all 0.3s;
        }

        .search-box:focus-within {
            border-color: #4ECDC4;
            box-shadow: 0 0 10px rgba(78, 205, 196, 0.2);
        }

        .search-box input {
            border: none;
            background: none;
            outline: none;
            padding: 8px;
            font-size: 14px;
            flex: 1;
        }

        .search-box button {
            background: none;
            border: none;
            color: #666;
            cursor: pointer;
            padding: 5px;
        }

        .category-filter {
            margin-bottom: 30px;
            text-align: center;
        }

        .filter-btn {
            background: #f8f9fa;
            border: 2px solid #dee2e6;
            color: #495057;
            padding: 8px 16px;
            margin: 0 5px;
            border-radius: 20px;
            cursor: pointer;
            transition: all 0.3s;
            font-size: 14px;
        }

        .filter-btn.active {
            background: #4ECDC4;
            border-color: #4ECDC4;
            color: white;
        }

        .filter-btn:hover {
            background: #e9ecef;
        }

        .filter-btn.active:hover {
            background: #44A08D;
        }

        .posts-list {
            background: white;
        }

        .post-item {
            border: 1px solid #e9ecef;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            transition: all 0.3s;
            cursor: pointer;
        }

        .post-item:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 20px rgba(0,0,0,0.1);
            border-color: #4ECDC4;
        }

        .post-header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            margin-bottom: 15px;
        }

        .post-title {
            font-size: 1.3em;
            font-weight: bold;
            color: #333;
            margin-bottom: 5px;
            line-height: 1.4;
        }

        .post-category {
            display: inline-block;
            background: #4ECDC4;
            color: white;
            padding: 4px 12px;
            border-radius: 15px;
            font-size: 12px;
            margin-bottom: 10px;
        }

        .post-content {
            color: #666;
            line-height: 1.6;
            margin-bottom: 15px;
            display: -webkit-box;
            -webkit-line-clamp: 3;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        .post-meta {
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 14px;
            color: #999;
        }

        .post-author {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .author-avatar {
            width: 30px;
            height: 30px;
            border-radius: 50%;
            background: linear-gradient(135deg, #4ECDC4 0%, #44A08D 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
        }

        .post-stats {
            display: flex;
            gap: 15px;
            align-items: center;
        }

        .stat-item {
            display: flex;
            align-items: center;
            gap: 5px;
        }

        .like-btn {
            background: none;
            border: none;
            color: #999;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 5px;
            padding: 5px;
            border-radius: 5px;
            transition: all 0.3s;
        }

        .like-btn:hover {
            background: #f8f9fa;
            color: #4ECDC4;
        }

        .like-btn.liked {
            color: #e74c3c;
        }

        .pagination {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 30px;
            gap: 10px;
        }

        .page-btn {
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            color: #495057;
            padding: 8px 12px;
            border-radius: 5px;
            text-decoration: none;
            transition: all 0.3s;
        }

        .page-btn:hover {
            background: #e9ecef;
            text-decoration: none;
            color: #495057;
        }

        .page-btn.active {
            background: #4ECDC4;
            border-color: #4ECDC4;
            color: white;
        }

        .empty-message {
            text-align: center;
            padding: 50px;
            color: #666;
            font-size: 1.2em;
        }

        @media (max-width: 768px) {
            .forum-actions {
                flex-direction: column;
                align-items: stretch;
            }
            
            .search-box {
                width: 100%;
            }
            
            .filter-btn {
                display: block;
                margin: 5px 0;
                width: 100%;
            }
            
            .post-header {
                flex-direction: column;
                align-items: flex-start;
            }
            
            .post-meta {
                flex-direction: column;
                align-items: flex-start;
                gap: 10px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>环保社区</h1>
            <p>分享环保心得，交流垃圾分类技巧</p>
        </div>

        <div class="content">
            <a href="{{ url_for('index') }}" class="back-btn">← 返回首页</a>
            
            <div class="forum-actions">
                <a href="{{ url_for('new_post') }}" class="new-post-btn">+ 发布新帖</a>
                
                <div class="search-box">
                    <input type="text" placeholder="搜索帖子..." id="searchInput">
                    <button onclick="searchPosts()">🔍</button>
                </div>
            </div>

            <div class="category-filter">
                <button class="filter-btn active" onclick="filterPosts('all')">全部</button>
                <button class="filter-btn" onclick="filterPosts('tips')">环保心得</button>
                <button class="filter-btn" onclick="filterPosts('question')">问题求助</button>
                <button class="filter-btn" onclick="filterPosts('share')">经验分享</button>
                <button class="filter-btn" onclick="filterPosts('discussion')">讨论交流</button>
            </div>

            <div class="posts-list">
                {% for post in posts %}
                <div class="post-item" data-category="{{ post.category }}" onclick="viewPost({{ post.id }})">
                    <div class="post-header">
                        <div>
                            <div class="post-category">{{ post.category }}</div>
                            <div class="post-title">{{ post.title }}</div>
                        </div>
                    </div>
                    
                    <div class="post-content">
                        {{ post.content | truncate(200) }}
                    </div>
                    
                    <div class="post-meta">
                        <div class="post-author">
                            <div class="author-avatar">{{ post.author.username[0].upper() }}</div>
                            <span>{{ post.author.username }}</span>
                            <span>{{ post.created_at.strftime('%Y-%m-%d %H:%M') }}</span>
                        </div>
                        
                        <div class="post-stats">
                            <div class="stat-item">
                                <button class="like-btn" onclick="event.stopPropagation(); toggleLike({{ post.id }}, this)">
                                    ❤️ <span>{{ post.like_count }}</span>
                                </button>
                            </div>
                            <div class="stat-item">
                                💬 {{ post.reply_count }}
                            </div>
                            <div class="stat-item">
                                👁️ {{ post.views_count }}
                            </div>
                        </div>
                    </div>
                </div>
                {% endfor %}
                
                {% if not posts %}
                <div class="empty-message">
                    <p>还没有帖子，快来发布第一个帖子吧！</p>
                </div>
                {% endif %}
            </div>

            {% if pagination %}
            <div class="pagination">
                {% if pagination.has_prev %}
                    <a href="{{ url_for('forum', page=pagination.prev_num) }}" class="page-btn">上一页</a>
                {% endif %}
                
                {% for page_num in pagination.iter_pages() %}
                    {% if page_num %}
                        {% if page_num != pagination.page %}
                            <a href="{{ url_for('forum', page=page_num) }}" class="page-btn">{{ page_num }}</a>
                        {% else %}
                            <span class="page-btn active">{{ page_num }}</span>
                        {% endif %}
                    {% else %}
                        <span class="page-btn">...</span>
                    {% endif %}
                {% endfor %}
                
                {% if pagination.has_next %}
                    <a href="{{ url_for('forum', page=pagination.next_num) }}" class="page-btn">下一页</a>
                {% endif %}
            </div>
            {% endif %}
        </div>
    </div>

    <script>
        function filterPosts(category) {
            const posts = document.querySelectorAll('.post-item');
            const buttons = document.querySelectorAll('.filter-btn');
            
            // 更新按钮状态
            buttons.forEach(btn => btn.classList.remove('active'));
            event.target.classList.add('active');
            
            // 过滤帖子
            posts.forEach(post => {
                if (category === 'all' || post.dataset.category === category) {
                    post.style.display = 'block';
                } else {
                    post.style.display = 'none';
                }
            });
        }

        function searchPosts() {
            const searchTerm = document.getElementById('searchInput').value.toLowerCase();
            const posts = document.querySelectorAll('.post-item');
            
            posts.forEach(post => {
                const title = post.querySelector('.post-title').textContent.toLowerCase();
                const content = post.querySelector('.post-content').textContent.toLowerCase();
                
                if (title.includes(searchTerm) || content.includes(searchTerm)) {
                    post.style.display = 'block';
                } else {
                    post.style.display = 'none';
                }
            });
        }

        function viewPost(postId) {
            window.location.href = `/forum/post/${postId}`;
        }

        function toggleLike(postId, button) {
            fetch(`/forum/like/${postId}`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                }
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    const countSpan = button.querySelector('span');
                    countSpan.textContent = data.likes_count;
                    
                    if (data.liked) {
                        button.classList.add('liked');
                    } else {
                        button.classList.remove('liked');
                    }
                }
            })
            .catch(error => {
                console.error('点赞失败:', error);
            });
        }

        // 搜索框回车事件
        document.getElementById('searchInput').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                searchPosts();
            }
        });
    </script>
</body>
</html>